package cmsparse;

import cmsparse.bean.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * Created by wangxingsheng on 2019/3/19.
 */
public class CmsParser {
    public static PagerParam packageList2PagerParam(List<PackageListBean> packageListBeanList) {
        int size;
        if (packageListBeanList != null && (size = packageListBeanList.size()) > 0) {
            PagerParam pagerParam = new PagerParam();
            LinkedHashMap<String, List<VideoParam>> videoMap = new LinkedHashMap<>();
            ArrayList<CmsItemsBean> infoList = new ArrayList<>();
            for (int i = 0; i < size; i++) {
                PackageListBean packageListBean = packageListBeanList.get(i);
                CmsItemsBean cmsItemsBean = getFirst(packageListBean);
                if (cmsItemsBean == null) {
                    continue;
                }
                switch (i) {
                    case 0:// 视频详情组件
                        pagerParam.setVideoStatu(cmsItemsBean.getVideoStatus());
                        pagerParam.setBaseInfo(cmsItemsBean);
                        if (pagerParam.getVideoStatu() == VideoStatu.PLAYBACK) {
                            List<VideoParam> videoParams = new ArrayList<>();
                            for (CmsItemsBean item : packageListBean.getComponentList()) {
                                if (!TextUtils.isEmpty(item.getVideoPlayBackUrl())) {
                                    VideoParam videoParam = new VideoParam(item.getTitle(), item.getVideoPlayBackUrl(), null, null);
                                    videoParams.add(videoParam);
                                }
                            }
                            if (videoParams.size() > 0) {
                                videoMap.put("精彩回放", videoParams);
                                infoList.add(cmsItemsBean);
                            }
                        }
                        break;
                    case 2:// 11频道组件
                    case 3:// 12频道组件
                    case 4:// 备用频道组件1
                    case 5:// 备用频道组件2
                        if (pagerParam.getVideoStatu() == VideoStatu.LIVING) {
                            List<VideoParam> videoParams = new ArrayList<>();
                            if (!TextUtils.isEmpty(cmsItemsBean.getDestinationUrl())) {
                                VideoParam videoParam = new VideoParam(cmsItemsBean.getTitle(), cmsItemsBean.getDestinationUrl(), null, null);
                                videoParams.add(videoParam);
                                videoMap.put(cmsItemsBean.getTitle(), videoParams);
                                int count = packageListBean.getComponentList().size();
                                if (count > 1) {
                                    infoList.add(packageListBean.getComponentList().get(1));
                                }
                            }
                        }
                        break;
                    case 6:// 频道限时优惠组件
                        CmsItemsBean[] favorableArray = new CmsItemsBean[]{new CmsItemsBean(), new CmsItemsBean()};
                        for (CmsItemsBean item : packageListBean.getComponentList()) {
                            if (item != null) {
                                if (72L == item.getComponentId()) {
                                    favorableArray[0] = item;
                                } else if (2L == item.getComponentId()) {
                                    favorableArray[1] = item;
                                }
                            }
                        }
                        pagerParam.setFavorableList(Arrays.asList(favorableArray));
//                        ArrayList<CmsItemsBean> favorableList = new ArrayList<>(packageListBean.getComponentList());
//                        pagerParam.setFavorableList(favorableList);
                        break;
                    case 1:// 推荐视频列表组件
                        break;
                }
            }
            pagerParam.setVideoParamsListMap(videoMap);
            pagerParam.setInfoList(infoList);
            return pagerParam;
        }
        return null;
    }

    private static CmsItemsBean getFirst(PackageListBean packageListBean) {
        if (packageListBean != null
                && packageListBean.getComponentList() != null
                && packageListBean.getComponentList().size() > 0) {
            return packageListBean.getComponentList().get(0);
        }
        return null;
    }
}
